﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace 数字孪生Demo.Model3D
{
    /// <summary>
    /// 网状围栏
    /// </summary>
    public class FenceModel3D : UIElement3D
    {
        private GeometryModel3D postModel = new GeometryModel3D();
        private GeometryModel3D fenceModel = new GeometryModel3D();

        public FenceModel3D()
        {
            postModel.Material = MaterialHelper.CreateMaterial(Colors.Red, 0.5);
            InitModel();

            Model3DGroup group = new Model3DGroup();    
            group.Children.Add(postModel);
            group.Children.Add(fenceModel);
            this.Visual3DModel = group;
        }

        #region 公共属性
        public double PostDiameter { get; set; } = 0.05;
        public double PostHeight { get; set; } = 2;
        public double PostDistance { get; set; } = 2;
        public double MeshSize { get; set; } = 0.06;
        #endregion

        #region 依赖属性

        public Brush FenceTexture
        {
            get { return (Brush)GetValue(FenceTextureProperty); }
            set { SetValue(FenceTextureProperty, value); }
        }

        public static readonly DependencyProperty FenceTextureProperty =
            DependencyProperty.Register("FenceTexture", typeof(Brush), typeof(FenceModel3D),
                new PropertyMetadata(null, (s,e)=> (s as FenceModel3D).UpdateFenceTexture()));



        public Point3DCollection Positions
        {
            get { return (Point3DCollection)GetValue(PositionsProperty); }
            set { SetValue(PositionsProperty, value); }
        }

        public static readonly DependencyProperty PositionsProperty =
            DependencyProperty.Register("Positions", typeof(Point3DCollection), typeof(FenceModel3D), 
                new PropertyMetadata(new Point3DCollection(),(s,e)=>(s as FenceModel3D).InitModel()));


        #endregion

        #region 依赖方法
        private void UpdateFenceTexture()
        {
            fenceModel.Material = FenceTexture != null ? new DiffuseMaterial(FenceTexture) { AmbientColor = Colors.White } : null;
            fenceModel.BackMaterial = FenceTexture != null ? new DiffuseMaterial(FenceTexture) { AmbientColor = Colors.White } : null;
        }

        private void InitModel()
        {
            
        }


        #endregion
    }
}
